#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../inc/data.h"
#include "../inc/my_flight_Node.h"
#include "../inc/general.h"
#include "../inc/user_node.h"

my_flight_Node_p my_flight_Init()
{
    my_flight_Node_p headNode = my_flight_create();
    if (!headNode)
        return NULL;

    // 初始化空链表：头节点自循环
    headNode->next = headNode;
    headNode->prev = headNode;

    my_flight_t my_flight[] = {

        {"张三", "中国国航", "CA1501", 0, 3380, 0},
        {"李四", "中国国航", "CA1501", 0, 3380, 0},
        {"王五", "中国国航", "CA1501", 1, 2880, 0},
        {"钱六", "中国国航", "CA1501", 1, 2880, 0},
        {"路人丁", "中国国航", "CA1501", 2, 1480, 0},
        {"路人丙", "中国国航", "CA1501", 2, 1480, 0},
        {"张四", "中国国航", "CA1501", 3, 3980, 0},
        {"王九", "中国国航", "CA1501", 3, 3980, 0},
        {"刘七", "中国国航", "CA1501", 4, 3180, 0},
        {"帅哥", "中国国航", "CA1501", 4, 3180, 0},

        {"user", "东方航空", "MU5101", 0, 3160, 1}, // 商务舱
        {"user", "南方航空", "CZ3101", 3, 4160, 1}, // 头等舱
        {"user", "中国国航", "CA1501", 2, 1480, 0}, // 经济舱

        // 路人甲的航班预订
        {"路人甲", "中国国航", "CA1501", 2, 1480, 0}, // 经济舱
        {"路人甲", "东方航空", "MU5101", 0, 3160, 1}, // 商务舱
        {"路人甲", "南方航空", "CZ3101", 3, 4160, 1}, // 头等舱

        // 路人乙的航班预订
        {"路人乙", "中国国航", "CA1501", 1, 2880, 0}, // 一等舱
        {"路人乙", "东方航空", "MU5101", 4, 2960, 1}, // 公务舱
        {"路人乙", "南方航空", "CZ3101", 2, 1660, 1}, // 经济舱

        {"张三", "中国国航", "CA1519", 0, 3380, 0},
        {"李四", "中国国航", "CA1519", 0, 3380, 0},
        {"王五", "中国国航", "CA1519", 1, 2880, 0},
        {"钱六", "中国国航", "CA1519", 1, 2880, 0},
        {"路人丁", "中国国航", "CA1519", 2, 1480, 0},
        {"路人丙", "中国国航", "CA1519", 2, 1480, 0},
        {"张四", "中国国航", "CA1519", 3, 3980, 0},
        {"王九", "中国国航", "CA1519", 3, 3980, 0},
        {"刘七", "中国国航", "CA1519", 4, 3180, 0},
        {"帅哥", "中国国航", "CA1519", 4, 3180, 0},
        {"user", "中国国航", "CA1519", 2, 1480, 0},   // 经济舱
        {"路人甲", "中国国航", "CA1519", 2, 1480, 0}, // 经济舱
        {"路人乙", "中国国航", "CA1519", 1, 2880, 0}  // 一等舱
    };

    int length = sizeof(my_flight) / sizeof(my_flight[0]);

    for (int i = 0; i < length; i++)
    {
        my_flight_Node_p node = my_flight_create();
        if (!node)
            continue;

        // 填充数据
        node->data = malloc(sizeof(my_flight_t));
        if (!node->data)
        {
            free(node);
            continue;
        }

        // 复制航班数据到节点
        memcpy(node->data, &my_flight[i], sizeof(my_flight_t));

        // 将新节点插入到链表尾部
        my_flight_Node_p tail = headNode->prev; // 当前尾节点

        tail->next = node;
        node->prev = tail;

        node->next = headNode; // 新节点指向头节点（循环）
        headNode->prev = node; // 头节点指向新尾节点
    }

    return headNode;
}

my_flight_Node_p my_flight_create()
{
    my_flight_Node_p currNode = (my_flight_Node_p)malloc(sizeof(my_flight_Node_t));
    currNode->data = NULL;
    currNode->next = currNode;
    currNode->prev = currNode;
    return currNode;
}

// 尾插
void my_flight_insert(my_flight_Node_p headNode, my_flight_p data)
{
    my_flight_Node_p newNode = my_flight_create();
    newNode->data = data;
    my_flight_Node_p tailNode = headNode->prev;
    tailNode->next = newNode;
    newNode->prev = tailNode;
    newNode->next = headNode;
    headNode->prev = newNode;
}

my_flight_Node_p find_my_flight_byUsernameAndFlightID(my_flight_Node_p headNode, char *username, char *flight_id)
{
    my_flight_Node_p currNode = headNode->next;
    while (currNode != headNode)
    {
        if (strcmp(currNode->data->username, username) == 0 && strcmp(currNode->data->flight_id, flight_id) == 0)
        {
            return currNode;
        }
        currNode = currNode->next;
    }
    return NULL;
}

void my_flight_delete(my_flight_Node_p currNode)
{
    // 删除节点
    currNode->prev->next = currNode->next;
    currNode->next->prev = currNode->prev;
    free(currNode->data);
    free(currNode);
}

my_flight_Node_p get_userFlightList(my_flight_Node_p headNode, char *uaername)
{
    my_flight_Node_p userHead = my_flight_create();
    my_flight_Node_p currNode = headNode->prev;

    while (currNode != headNode)
    {
        if (strcmp(currNode->data->username, uaername) == 0)
        {
            my_flight_p data = (my_flight_p)malloc(sizeof(my_flight_t));
            memcpy(data, currNode->data, sizeof(my_flight_t));
            my_flight_insert(userHead, data);
        }
        currNode = currNode->prev;
    }
    // my_flight_Node_p tempNode = userHead->next;
    // while (tempNode->next != userHead)
    // {
    //     my_flight_Node_p nextNode = currNode->next; // 保存下一个节点
    //     if (tempNode->data->status == 0)
    //     {
    //         my_flight_headInsert(userHead, tempNode);
    //     }
    //     tempNode = nextNode;
    // }
    return userHead;
}

// 神秘插入
void my_flight_headInsert(my_flight_Node_p headNode, my_flight_Node_p currNode)
{
    // 先把currNode抽离
    currNode->prev->next = currNode->next;
    currNode->next->prev = currNode->prev;

    my_flight_Node_p tempNode = headNode->next;
    // 再把currNode插入到最后应该status=0的后面
    while (tempNode->next != headNode)
    {
        if (tempNode->next->data->status != 0)
        {
            break;
        }
        tempNode = tempNode->next;
    }

    tempNode->prev->next = currNode;
    currNode->prev = tempNode->prev;
    currNode->next = tempNode;
    tempNode->prev = currNode;
}

int getMyFlightCount(my_flight_Node_p headNode)
{
    int count = 0;
    my_flight_Node_p currNode = headNode->next;
    while (currNode != headNode)
    {
        count++;
        currNode = currNode->next;
    }
    return count;
}

void free_my_flightList(my_flight_Node_p headNode)
{
    if (headNode == NULL)
    {
        return;
    }

    my_flight_Node_p currNode = headNode->next;
    while (currNode != headNode)
    {
        my_flight_Node_p tempNode = currNode;
        currNode = currNode->next;
        free(tempNode->data);
        free(tempNode);
    }
    free(headNode);
}

// 删除航班 把金额退给用户
void my_flight_deleteByFlight_id(my_flight_Node_p headNode, char *flight_id)
{
    my_flight_Node_p currNode = headNode->next;
    while (currNode != headNode)
    {
        if (strcmp(currNode->data->flight_id, flight_id) == 0)
        {
            user_Node_p user = find_userNode(user_headNode, currNode->data->username);
            if (user != NULL)
            {
                user->data->money += currNode->data->price; // 退钱
            }
            my_flight_delete(currNode);
        }
        currNode = currNode->next;
    }
}

//  座位调整后删除对应的用户订单
void find_my_flight_by_seat(my_flight_Node_p headNode, char *flight_id, int seat_type)
{
    my_flight_Node_p currNode = headNode->prev; // 尾部开始查询
    while (currNode != headNode)
    {
        if (strcmp(currNode->data->flight_id, flight_id) == 0 && currNode->data->seat_type == seat_type)
        {
            user_Node_p user = find_userNode(user_headNode, currNode->data->username);
            if (user != NULL)
            {
                user->data->money += currNode->data->price; // 退钱
            }

            my_flight_delete(currNode);
            break;
        }
        currNode = currNode->prev;
    }
}

// 管理员修改航班号对应的用户航班订单也要修改
my_flight_p updata_my_flightByflight_id(my_flight_Node_p headNode, char *old_flight_id, char *new_flight_id)
{
    my_flight_Node_p currNode = headNode->prev;
    while (currNode != headNode)
    {
        if (strcmp(currNode->data->flight_id, old_flight_id) == 0)
        {
            strcpy(currNode->data->flight_id, new_flight_id);
        }
        currNode = currNode->prev;
    }
}

// 获取已预订用户的航班列表  --管理员视角
my_flight_Node_p get_bookedUsersFlightList(my_flight_Node_p headNode, char *flight_id)
{
    my_flight_Node_p bookedUsersHead = my_flight_create();
    my_flight_Node_p currNode = myflight_headNode->prev;

    while (currNode != myflight_headNode)
    {
        if (strcmp(currNode->data->flight_id, flight_id) == 0)
        {
            my_flight_p data = (my_flight_p)malloc(sizeof(my_flight_t));
            data = currNode->data;
            my_flight_insert(bookedUsersHead, data);
        }
        currNode = currNode->prev;
    }

    return bookedUsersHead;
}